import { defineStore } from 'pinia'
import { getBettingConfigs, getFreeBetList, getQuickInputList } from '@/api/betting'
import { useAppStore } from './app'
import { useMemberStore } from './member'
import { BettingMethod } from '@/enums'

export const useBettingConfigStore = defineStore('bettingConfig', {
	state: () => ({
		comboBonus: [
			{ percent: '3%', number: 3 },
			{ percent: '6%', number: 5 },
			{ percent: '9%', number: 7 },
			{ percent: '15%', number: 10 }
		],
		comboBonusMinTrueOdds: 1.5,
		comboBonusMinDisplayOdds: 1.5,
		validRfcOdds: 1.9,
		reasonMinChars: 25,
		bettingMin: 0,
		bettingMax: Number.MAX_VALUE,
		allFreeBetList: [],
		quickInputList: [],
		lastInputSingle: 0,
		lastInputCombo: 0
	}),
	actions: {
		async getConfigs() {
			try {
				const appStore = useAppStore()
				const memberStore = useMemberStore()
				const { data } = await getBettingConfigs({
					location: appStore.region
				})
				this.setComboBonus(data.comboBonusSetting)
				this.setMinChars(data.minCharacter)
				this.setBettingLimit(data)
				memberStore.setUserTagsDays(data.roiDays, data.winRateDays)
			} catch (error) {}
		},
		async getFreeBetList() {
			try {
				const { data } = await getFreeBetList()
				console.log(data)
				// const list = data.map(
				//   ({ bonusId, bonusName, worth, img, amount, assignIds, isRfc }) => ({
				//     id: bonusId,
				//     name: bonusName,
				//     amount: worth,
				//     num: amount,
				//     img: img ? import.meta.env.VUE_APP_IMAGE_URL + img : freeBetImgDefault,
				//     assignIds,
				//     isRfc
				//   })
				// )
				// this.getFreeBetList = list
			} catch {}
		},
		async getQuickInputList() {
			try {
				const appStore = useAppStore()
				const { data } = await getQuickInputList({
					location: appStore.region
				})
				if (data && data.length) {
					this.quickInputList = data
				}
			} catch {}
		},
		setComboBonus(value: any) {
			if (value) {
				this.comboBonus = value
			}
		},
		setMinChars(value: number) {
			if (value) {
				this.reasonMinChars = value
			}
		},
		setBettingLimit(value: any) {
			if (value.minStake) {
				this.bettingMin = value.minStake
			}
			if (value.maxStake) {
				this.bettingMax = value.maxStake
			}
		},
		setLastInput(type: BettingMethod, val: number) {
			if (type === BettingMethod.single) {
				this.lastInputSingle = val
			} else if (type === BettingMethod.combo) {
				this.lastInputCombo = val
			}
		}
	},
	getters: {},
	persist: true
})
